home *** CD-ROM | disk | FTP | other *** search
- \ String Art demo. Load this file then type stringart
- \ Typing any key stops the demo.
-
- needs rnd random.fth
- needs line-a-init linea.fth
- line-a-init
-
- decimal
-
- 13 constant #functions
- 343 constant #artlines
-
- #functions #artlines * constant #points
-
- create function-points #points /w* allot
-
- : random ( -- n ) #functions rnd ;
-
- \ Get a new random number that is different from the old one
- : new-rand ( old-rand -- new-rand )
- begin random ( old new )
- 2dup =
- while drop
- repeat
- nip
- ;
-
- : write-binary-points ( -- )
- [""] stringpt.bin dup make drop
- write open ofd !
- function-points #points /w* ofd @ fputs
- ofd @ close
- ;
- defer test ' noop is test
-
- \ Read the ascii version of the function tables and write it back out
- \ as a binary file
- : read-points ( -- )
- [""] stringpt.num read open ifd !
- hex
- function-points #points /w*
- bounds
- ?do
- pad ifd @ getword test
- number? 0= abort" bogus"
- i w!
- /w +loop
- ifd @ close
- write-binary-points
- ;
-
- \ Read in the binary version of the function tables
- : read-binary-points ( -- )
- [""] stringpt.bin read open ifd !
- function-points #points /w* tuck ifd @ fgets
- <> if ." Read failed" cr then
- ifd @ close
- ;
- variable xs variable ys \ Starting endpoint for a line
- variable xe variable ye \ Ending endpoint for a line
-
- \ Find the starting address for the index'th function in the function
- \ table
- : >function ( index -- table-address )
- #artlines * /w* function-points +
- ;
-
- \ Coefficients for transforming to the screen coordinate system
- wvariable xscale wvariable yscale
- wvariable xoffset wvariable yoffset
- : set-scaling ( -- )
- get-rez ( xmax ymax )
- 2dup
- 9 10 */ yscale w!
- 9 10 */ xscale w! ( xmax ymax )
- 20 / yoffset w!
- 20 / xoffset w!
- ;
- \ Transform normalized device coordinates to screen coordinates
- code ndc>device ( x y -- x' y' )
- sp )+ d1 move \ y
- sp )+ d0 move \ x
- xscale l#) d0 mulu
- yscale l#) d1 mulu
- d0 d0 add
- d1 d1 add
- d0 word clr normal
- d0 swap
- d1 word clr normal
- d1 swap
- word xoffset l#) d0 add normal
- word yoffset l#) d1 add normal
- d0 sp -) move
- d1 sp -) move
- c;
-
- : nextw ( variable -- w )
- dup @ w@ /w rot +!
- ;
- : draw-line ( -- )
- xs nextw ys nextw ndc>device ( startxy )
- xe nextw ye nextw ndc>device ( startxy endxy )
- draw
- ;
- : string-drawing ( xs xe yx ye -- )
- >function ye ! >function ys ! >function xe ! >function xs !
- #artlines 0 do draw-line loop
- ;
- : new-drawing ( -- )
- _fg_bp_1 w@ ( foreground-color )
-
- random dup new-rand ( color xs xe )
- random dup new-rand ( color xs xe ys ye )
-
- \ Draw with the foreground color
- 4dup string-drawing ( color xs xe ys ye )
-
- \ Erase by drawing with the background color
- 0 _fg_bp_1 w!
- string-drawing ( color )
-
- \ Restore the foreground color
- _fg_bp_1 w!
- ;
- : stringart-setup ( -- )
- set-scaling
- 0 _wrt_mod w!
- erase-screen
- ;
- : stringart ( -- )
- stringart-setup
- begin new-drawing key? until
- ;
- read-binary-points
-